package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.MyGroupModel;
import com.hangzhou.xyj.ui.listener.RecycleOnItemClickListener;
import com.hangzhou.xyj.ui.widget.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neo.duan on 2016/1/11.
 * 我的圈子适配器
 */
public class MyGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutInflater inflater;
    private List<MyGroupModel> mDatas;
    private RecycleOnItemClickListener mListener;


    public MyGroupAdapter(Context context, List<MyGroupModel> datas) {
        this.mContext = context;
        if (datas == null) {
            datas = new ArrayList<>();
        }
        this.mDatas = datas;
        inflater = LayoutInflater.from(context);
    }

    public void update(List<MyGroupModel> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mDatas = list;
        notifyDataSetChanged();
    }

    /**
     * 对外提供方法，设置回调接口
     *
     * @param listener
     */
    public void setOnRecyclerViewItemClickListener(RecycleOnItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getGroupType();
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 绑定Header下面数据的ViewHolder
     *
     * @param viewHolder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder == null) {
            return;
        }
        GroupItemViewHolder holder = (GroupItemViewHolder) viewHolder;
        holder.setData(mDatas.get(position));
    }

    /**
     * 创建Header下面数据的ViewHolder
     *
     * @param viewGroup
     * @param viewType
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = inflater.inflate(R.layout.layout_mygroup_item, viewGroup, false);
        return new GroupItemViewHolder(view);
    }

    @Override
    public long getHeaderId(int position) {
        return mDatas.get(position).getGroupType();
    }

    /**
     * 创建Headr部分的ViewHolder
     *
     * @param parent the view to create a header view holder for
     * @return
     */
    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = inflater.inflate(R.layout.layout_group_item_header, parent, false);
        return new HeaderItemViewHolder(view);
    }

    /***
     * 绑定header部分的数据
     *
     * @param holder   the view holder
     * @param position the adapter position
     */
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        HeaderItemViewHolder mHolder = (HeaderItemViewHolder) holder;
        switch (getItemViewType(position)) {
            case MyGroupModel.CREATE_GROUP:
                mHolder.mTvGroupName.setText("我创建的圈子");
                break;
            case MyGroupModel.ENTER_GROUP:
                mHolder.mTvGroupName.setText("我加入的圈子");
                break;
            default:
                break;
        }
    }

    /**
     * 圈子的ViewHolder类
     */
    class GroupItemViewHolder extends RecyclerView.ViewHolder {
        private MyImageView mImageView;//圈子的图标
        private MyTextView mTextView;//圈子的名称文本
        private MyGroupModel mGroupModel;

        public GroupItemViewHolder(View itemView) {
            super(itemView);
            mImageView = (MyImageView) itemView.findViewById(R.id.tv_group_img);
            mTextView = (MyTextView) itemView.findViewById(R.id.tv_group_name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(v, mGroupModel);
                    }
                }
            });
        }

        public void setData(MyGroupModel model) {
            this.mGroupModel = model;
            mImageView.displayImage(model.getGroupImage());
            mTextView.setText(model.getGroupName());
            //将需要在点击时的数据放到tag中
            mTextView.setTag(model.getGroupName());
        }

    }

    /**
     * Header部分的ViewHolder
     */
    class HeaderItemViewHolder extends RecyclerView.ViewHolder {
        MyTextView mTvMore;
        MyTextView mTvGroupName;

        public HeaderItemViewHolder(View itemView) {
            super(itemView);
            mTvGroupName = (MyTextView) itemView.findViewById(R.id.tv_header_group);
            mTvMore = (MyTextView) itemView.findViewById(R.id.tv_more);
            //隐藏”更多“
            mTvMore.setVisibility(View.GONE);
        }
    }
}
